<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/common.jsp"%>
<%
	//查询人均费用
	String ksdm = input.getDesc("ksdm","-1"); //传入科室代码代表该科室主任
	String date = input.getDesc("start_date","2018-04-16");//开始时间
	String endDa = input.getDesc("end_date","2018-04-16");//结束时间
	String yydm =  input.getDesc("yydm","01");//医院代码
	
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
	String nowDate = sdf2.format(sdf.parse(date));
	String nowEndDa = sdf2.format(sdf.parse(endDa));
	
	String startDate = nowDate+"00:00:00";
	String endDate = nowEndDa+"23:59:59";

	String sqlWhere = "  " ;
		
	String sqlWhereDept = "";
	if(!"-1".equals(ksdm)){
		sqlWhere += " and a.ksdm in(" + ksdm + ")";
		sqlWhereDept = " and id in("+ksdm + ")";
	}
	//首先查询总人数
	String sqlQue = "";
	if(!yydm.equals("-1")){
		sqlQue = "select y.id as id ,y.name as name  ,case  when cont IS NULL  then 0 ELSE  g.[cont] END cont  from (select id ,name,yydm from YY_KSBMK where yydm ='"+yydm+"' "+sqlWhereDept +"   ) y RIGHT JOIN (select g.ksdm,g.ksmc , count(*) as cont  from  VW_GHZDK g  where ghrq >'"+ startDate +"' and ghrq <'" + endDate + "' and g.jlzt=0 GROUP BY ksdm ,ksmc ) g  on g.ksdm = y.id where y.yydm ='"+yydm+"'  ORDER BY g.cont desc ";
		sqlWhere += " and a.ksdm in (select id from YY_KSBMK where  yydm ='"+yydm+"') " ;
	
	}else
		sqlQue = "select y.id as id ,y.name as name  ,case  when cont IS NULL  then 0 ELSE  g.[cont] END cont  from (select id ,name,yydm from YY_KSBMK where 1=1 "+sqlWhereDept +"   ) y RIGHT JOIN (select g.ksdm,g.ksmc , count(*) as cont  from  VW_GHZDK g  where ghrq >'"+ startDate +"' and ghrq <'" + endDate + "' and g.jlzt=0 GROUP BY ksdm ,ksmc ) g  on g.ksdm = y.id   ORDER BY g.cont desc ";
	
	String mzsql = "\n" +
		"select a.ksdm , sum(convert(numeric(20,4),case when  d.ypbz in (1,2,3) then (case when 0=1 then b.ylsj else b.ylsj-b.yhdj end)*b.ypsl*b.ts*b.cfts/b.ykxs else 0 end)) ypje    \n" +
		",sum(convert(numeric(20,4),case when  d.ypbz not in (1,2,3) then (case when 0=1 then b.ylsj else b.ylsj-b.yhdj end)*b.ypsl*b.ts*b.cfts/b.ykxs else 0 end)) mzylsr     \n" +
		"from VW_MZCFK a (nolock)  \n" +
		"inner join  VW_CFMXK b (nolock) on b.cfxh = a.xh   \n" +
		"inner join  YY_YBFLK c (nolock) on a.ybdm = c.ybdm  \n" +
		"inner join YY_SFDXMK d (nolock) on d.id = b.dxmdm    \n" +
		"where a.lrrq between  '"+nowDate+"00000' and '"+nowEndDa+"24000'  \n" +sqlWhere +" \n"+
		" and a.jsbz = 1     \n" +
		" and a.jlzt in (0,1,2,3,4)       \n" +
		"group by substring(a.lrrq,1,8),a.ksdm  \n";
		//0:正常，1:退方,2红冲,3:家床取消,4：家床取消红冲 
		
	List<Map> resultList = null;
	StatelessSession ss =  DBTools.db("basedb").getSf().openStatelessSession();
    double num = 0;
	try {
		 ss.beginTransaction();
		 SQLQuery sqlQuery = ss.createSQLQuery(sqlQue);
		 sqlQuery.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
		 List<Map> list = sqlQuery.list();
		
		 for(Map map : list){
			 String con = map.get("cont") == null ? "0" : map.get("cont").toString();
			 num += Integer.parseInt(con);
		 }
		 
		 SQLQuery lbsqlQuery = ss.createSQLQuery(mzsql);
		 lbsqlQuery.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
         resultList = lbsqlQuery.list();
		 
		  ss.getTransaction().commit();
		 ss.close();
    }catch (Exception e){
	    ss.getTransaction().commit();
		 ss.close();
    }
	num =  num == 0 ? 1 : num;
	//List<Map> countList =QBuilder.db("basedb").queryHql("select new map(count(distinct js.ghxh) as cont) from SF_BRJSK js where 1=1 and  js.jlzt = 0 " + sqlWhere +" ");

	//费用明细集合
	//List<Map> feelist = QBuilder.db("basedb").queryHql("select new map( js.sfrq as sfrq , js.zje as xmje,js.ghxh as ghxh,js.ksdm as ksdm) from  SF_BRJSK js where js.ybjszt=2 and js.jlzt = 0 " + sqlWhere);

	

   
	System.out.println(mzsql);
	double money = 0;
	 if(resultList != null && resultList.size() > 0){
		 for(Map reMap : resultList){
			 double ypje = Double.parseDouble(reMap.get("ypje").toString());//药品收入  
			 double mzylsr = Double.parseDouble(reMap.get("mzylsr").toString());//门诊医疗收入 
			 money = money + ypje + mzylsr ;
		 }
	 }
	System.out.println(money);
	double average = 0;
	
	average = money/num;
	DecimalFormat df = new DecimalFormat("######0.00"); 
	
	Map<String,Object> ma = new HashMap<String,Object>();
	Map<String,Object> ma1 = new HashMap<String,Object>();
	ma.put("sumCount",df.format(average));
	ma1.put("data",ma);
	Message msg = new Message();

	msg.put("errcode",0);
	msg.put("average",ma1);
	
	out.println(msg);
%>